package mgutil

import (
	"car_rental/shared/mongo/objid"
	"car_rental/shared/typings"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"time"
)

const (
	IdFieldName        = "_id"
	UpdatedAtFieldName = "updatedat"
)

// IdField  主键ID
type IdField struct {
	Id primitive.ObjectID `bson:"_id"`
}

type UpdatedAtField struct {
	UpdatedAt int64 `bson:"updatedat"`
}

var NewObjId = primitive.NewObjectID

func NewObjIdWithValue(id fmt.Stringer) {
	NewObjId = func() primitive.ObjectID {
		return objid.MustFromId(id)
	}
}

var UpdatedAt = func() int64 {
	return time.Now().UnixNano()
}

// Set Mongo设置值 $set
func Set(t typings.T) bson.M {
	return bson.M{
		"$set": t,
	}
}

func SetOneInsert(t typings.T) bson.M {
	return bson.M{
		"$setOnInsert": t,
	}
}
